package com.atguigu.zhxy.controller;

import com.atguigu.zhxy.pojo.Admin;
import com.atguigu.zhxy.service.AdminService;
import com.atguigu.zhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
@Api(tags = "管理员控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
    @Autowired
    private AdminService adminService ;
    @ApiOperation("新增或修改Grade，有id属性就是修改，没有则是新增")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(
            @ApiParam("Json的admin对象")@RequestBody Admin admin){
        adminService.saveOrUpdate(admin) ;
        return Result.ok();
    }

    @ApiOperation("根据年级名称模糊查询，带分页")
    @GetMapping("/getAllAdmin/{pageNum}/{pageSize}")
    public Result getAllAdmin(
            @ApiParam("分页查询的页码数")@PathVariable("pageNum") Integer pageNum,
            @ApiParam("分页查询的记录数")@PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询模糊匹配的名称")@Param("adminName") String adminName
    ){
//        分页带条件查询
        Page<Admin> page = new Page<>(pageNum, pageSize);
//        通过服务层
        IPage<Admin> AdminIPage = adminService.getAdminByPN(page,adminName);
//        封装result对象并返回
        return Result.ok(AdminIPage) ;
    }

    @ApiOperation("删除Grade")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(
            @ApiParam("要删除的所有的grade的id的JSON集合") @RequestBody ArrayList<Integer> idList){
        adminService.removeByIds(idList) ;
        return Result.ok();
    }
}
